package com.geekhalo.domainevent.v1;

import com.geekhalo.domainevent.service.*;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AllInOneMethod implements OrderService {
    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private CartService cartService;

    @Autowired
    private SMSService smsService;

    @Autowired
    private RecommendService recommendService;

    @Override
    @Transactional
    public void createOrder(CreateOrderCommand createOrderCommand) {
        // 1. 处理订单逻辑
        Preconditions.checkArgument(createOrderCommand.isValid());

        Order order = Order.create(createOrderCommand);

        this.orderRepository.save(order);

        // 2. 发送短信通知
        SendSMSCommand sendSMSCommand = buildSendSMSCommand...
        this.smsService.send(sendSMSCommand);


        // 3. 清空购物车
        CleanCartCommand cleanCartCommand = buildCleanCartCommand...
        this.cartService.clean(cleanCartCommand);


        // 4. 重新计算推荐
        RecalculateRecommendCommand recalculateRecommendCommand = buildRecalculateRecommendCommand...
        this.recommendService.recalculate(recalculateRecommendCommand);

    }
}
